package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.Course;
import br.com.gaacorp.sai.webapp.action.BasePage;
import br.com.gaacorp.sai.service.GenericManager;

public class CourseForm extends BasePage implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 3479132515191103656L;
	private GenericManager<Course, Long> courseManager;
    private Course course = new Course();
    private Long courseId;

    public void setCourseManager(GenericManager<Course, Long> courseManager) {
        this.courseManager = courseManager;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public void setCourseId(Long courseId) {
        this.courseId = courseId;
    }

    public String delete() {
        courseManager.remove(course.getCourseId());
        addMessage("course.deleted");

        return "courseList";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (courseId != null && courseId != 0) {
            course = courseManager.get(courseId);
        } else {
            course = new Course();
        }

        return "courseForm";
    }

    public String save() {
        boolean isNew = (course.getCourseId() == null || course.getCourseId() == 0);
        courseManager.save(course);

        String key = (isNew) ? "course.added" : "course.updated";
        addMessage(key);

        if (isNew) {
            return "courseList";
        } else {
            return "courseForm";
        }
    }
} 